package com.echo.util;

import com.google.gson.TypeAdapter;
import com.google.gson.stream.JsonReader;
import com.google.gson.stream.JsonToken;
import com.google.gson.stream.JsonWriter;

import java.io.IOException;

public class StringTypeAdapter extends TypeAdapter<String> {

	@Override
	public void write(JsonWriter writer, String value)
			throws IOException {
		if (value == null) {
			writer.value("");
		}else{						
			writer.value(value);
		}
	}
	@Override
	public String read(JsonReader reader) throws IOException {
		if (reader.peek() == JsonToken.NULL) {
            reader.nextNull();
            return null;
        }else{
        	String value =  reader.nextString();
    		if("".equals(value)){
    			return null;
    		}else{
    			return value;
    		}
        }
		
	}

}
